package swing.admin.dao;

import swing.admin.model.Major;
import swing.admin.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MajorDao extends BaseMapper<Major> {
    public MajorDao() {
        super(Major.class);
    }

    public List<Major> listAll() {
        List<Major> list = selectList("select * from major", null);
        return list;
    }

    public List<Major> list(Major queryParam) {

        StringBuilder sql = new StringBuilder("select * from major where 1=1");
        ArrayList<Object> param = new ArrayList<>();

        if (!Objects.isNull(queryParam.getId())) {
            sql.append(" and id = ?");
            param.add(queryParam.getId());
        }

        if (!Objects.isNull(queryParam.getName())) {
            sql.append(" and name = ?");
            param.add(queryParam.getName());
        }

        System.out.println(sql);
        List<Major> list = selectList(sql.toString(), param.toArray());
        return list;
    }

    public Major getById(Integer id) {
        Major user = selectOne("select * from major where id = ?", id);
        return user;
    }

    public void deleteById(Integer id) {
        boolean delete = delete("delete from major where id = ?", id);
    }

    public void updateById(Major major){
        StringBuilder sql = new StringBuilder("update major");
        ArrayList<Object> param = new ArrayList<>();

        if (!Objects.isNull(major.getName())) {
            sql.append(" set name = ?");
            param.add(major.getName());
        }

        if (!Objects.isNull(major.getId())) {
            sql.append(" where id = ?");
            param.add(major.getId());
        }

        System.out.println(sql);

        int update = update(sql.toString(), param.toArray());
        System.out.println(update);
    }

    public void insert(Major major){
        ArrayList<Object> param = new ArrayList<>();
        param.add(major.getName());
        boolean insert = insert("insert into major(name) values(?)", param.toArray());
    }
}
